package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import enums.main.UISize;
	
	import starling.core.Starling;
	
	import view.component.main.MH4Wiki;
	
	/**
	 * MH4 WiKi 外层框架
	 * @author £iang
	 * 
	 */
	[SWF(width="960",height="640",frameRate="60",backgroundColor="#2f2f2f")]
	public class Main extends Sprite
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** Starling */
		private var _starling:Starling;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理主应用添加到舞台后派发的事件 
		 * @param event 主应用添加到舞台后派发的事件 
		 * 
		 */		
		protected function addedToStageHandler( event:Event ):void
		{
			//Starling.handleLostContext = true;
			Starling.multitouchEnabled = true;
			_starling = new Starling( MH4Wiki, stage );
			this._starling.enableErrorChecking = false;
			_starling.start();
			this.stage.addEventListener( Event.RESIZE, stage_resizeHandler, false, int.MAX_VALUE, true );
			//this.stage.addEventListener( Event.DEACTIVATE, stage_deactivateHandler, false, 0, true );
		}
		
		/**
		 * 处理窗口大小改变时派发的事件 
		 * @param event 窗口大小改变时派发的事件 
		 * 
		 */		
		private function stage_resizeHandler(event:Event):void
		{
			UISize.MAIN_W = this._starling.stage.stageWidth = this.stage.stageWidth;
			UISize.MAIN_H = this._starling.stage.stageHeight = this.stage.stageHeight;
			
			const viewPort:Rectangle = this._starling.viewPort;
			viewPort.width = this.stage.stageWidth;
			viewPort.height = this.stage.stageHeight;
			try
			{
				this._starling.viewPort = viewPort;
			}
			catch(error:Error) {}
		}
		
		/**
		 * 处理窗口失效时派发的事件 
		 * @param event 窗口失效时派发的事件 
		 * 
		 */		
		private function stage_deactivateHandler(event:Event):void
		{
			this._starling.stop();
			this.stage.addEventListener(Event.ACTIVATE, stage_activateHandler, false, 0, true);
		}
		
		/**
		 * 处理窗口激活时派发的事件 
		 * @param event 窗口激活时派发的事件 
		 * 
		 */		
		private function stage_activateHandler(event:Event):void
		{
			this.stage.removeEventListener(Event.ACTIVATE, stage_activateHandler);
			this._starling.start();
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function Main()
		{
			if( this.stage )
			{
				this.stage.scaleMode = StageScaleMode.NO_SCALE;
				this.stage.align = StageAlign.TOP_LEFT;
			}
			this.mouseEnabled = this.mouseChildren = false;
			addEventListener( Event.ADDED_TO_STAGE, addedToStageHandler );
		}
	}
}